<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/common.jspf" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test</title>


</head>

<script type="text/javascript">
    var flag = undefined;
  //初始化表单
    $(function (){
        var editIndex = undefined;
        $('#dg').datagrid({
            url:'',
            columns:[[
                {checkbox:true},
                {
                    editor: {
                        type: 'text',
                        options:'String'
                    }
                },
                {field:'menuScale',title:'规格参数',width:100,editor:'text'},
                {field:'menuStyle',title:'样式选择',width:100,align:'right',editor:'text'}
            ]],
            toolbar: [{
                iconCls: 'icon-save',
                text:'添加',
                handler: function(){
                  var id=$('#tt').tree('getSelected');
                  if(id==null){
                      alert("请选择最小类目")
                  }else{
                      var id=$('#tt').tree('getSelected');
                      var d=id.id;
                      var dd=id.text;
                      var row = {
                          menuScale:'点击修改',
                          menuStyle:'点击修改',
                          menuFather:d,
                          menuType:dd
                      };
                      $('#dg').propertygrid('appendRow',row);
                  }
                }
            },'-',{
                iconCls: 'icon-edit',
                text:'清除',
                handler: function(){
                      //获取当前行
                    var id=$('#tt').tree('getSelected');
                    if(id==null){
                        alert("请选择最小类目")
                    }else{
                        var deletedData = $('#dg').datagrid('getChecked');

                        for (var i = deletedData.length - 1; i >= 0; i--) {
                            var rowIndex = $('#dg').datagrid('getRowIndex', deletedData[i]);
                            $('#dg').datagrid('deleteRow', rowIndex); }
                    }
                }
            },'-',{
                iconCls: 'icon-remove',
                text:'保存所有',
                handler: function(){
                    var id=$('#tt').tree('getSelected');
                    if(id==null){
                        alert("请选择最小类目")
                    }else{
                      var q= $('#dg').datagrid('getData');
                        var aaa=JSON.stringify(q.rows)
                        if(aaa=='[]'){
                            alert("请添加数据再保存")
                        }else{
                            $.post(
                                "${proPath}/seemenu/insert.action",
                                {aaa:aaa},
                                "json",
                                $.messager.alert('提示','操作成功')
                            );
                        }
                    }
                }
            },'-',{
                iconCls: 'icon-remove',
                text:'清除所有',
                handler: function(){
                    var id=$('#tt').tree('getSelected');
                    if(id==null){
                        alert("请选择最小类目")
                    }else{
                        var d=$('#tt').tree('getSelected');
                        var id=d.id;
                        alert(id)
                        $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                            if (r){
                                alert('确认删除');
                                $.post(
                                    "${proPath}/seemenu/delete.action?id="+id+"",
                                    {},
                                    function (data) {

                                        if(data==0){
                                            //刷新原页面
                                            $('#dg').datagrid("reload");
                                        }
                                    },
                                    "json"
                                );
                            }
                        });
                    }

                }
            }],
            striped:true,
            rownumbers:true,
            pagination:true,
            fitColumns:true,
            pageSize:15,
            pageList:[5,10,15,20,25],
            /* queryParams: {
             userName: 'easyui',
             userAddress: 'datagrid'
             } */

            //双击修改
                onDblClickCell: function(index,field,value){
                    // alert(index+field+value)
                    $(this).datagrid('beginEdit', index);
                    editIndex = index;
                    // var ed = $(this).datagrid('getEditor', {index:index,field:field});
                    // $(ed.target).focus();
                },
                onClickCell: function(index,field,value){
                    // alert(index+field+value)
                    $(this).datagrid('endEdit', editIndex);
                    editIndex == undefined;
                    // var ed = $(this).datagrid('getEditor', {index:index,field:field});
                    // $(ed.target).focus();
                }
        });
    });

    //点击菜单写入表单
    $(function () {
        $('#tt').tree({
            url:'${proPath}/seemenu/seemen.action' ,

            // 在用户点击的时候提示
            onClick: function(node){
                var type_name=node.text;
                var id=node.id;
                $("#wz").val(type_name);
                $("#wzz").val(type_name);
                $("#fl").val(id);
                if(id==1){

                }
                else{
                    $('#dg').datagrid({
                        url:'${proPath}/seemenu/seeh.action?id='+id+'',
                        columns:[[
                            {checkbox:true},
                            {field:'menuScale',title:'规格参数',width:100,editor:'text'},
                            {field:'menuStyle',title:'样式选择',width:100,align:'right',editor:'text'}
                        ]],
                    });
                }
            },
            // 右键点击节点并显示快捷菜单
            onContextMenu: function(e, node){
                /*var a = $('#tt').tree('getNode', node.target);*/
                e.preventDefault();
                // 查找节点
                $('#tt').tree('select', node.target);
                // 显示快捷菜单
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }

        });
    });
 //读取菜单
    $(function () {
        $('#tt').tree({
            url:'${proPath}/seemenu/seemen.action' ,

    });
    });

  //点击触发事件
    $(function () {

        $('#tt').tree({
            onAfterEdit:function (node) {
             if(flag=='2'){
                 var d = $('#tt').tree('getSelected');
                 var id = d.id;
                 var name = d.text;
                 $.post(
                     "${proPath}/seemenu/ademnds.action",
                     {id: id,name:name},
                     "json"
                 );
                 $('#tt').tree('reload')
                 alert("修改成功")
             }else {
                 var selected = $('#tt').tree('getSelected');
                 var name=node.text;
                 $.post(
                     "${proPath}/seemenu/addMenuPlan.action",
                     {name:name},
                     "json"
                 );
                 $('#tt').tree('reload');
                 alert("添加成功")
             }

            }
        })

    })
//删除
 function remove() {
     var d=$('#tt').tree('getSelected');
     var id=d.id;
        if(id==1){
            $.messager.alert('警告','此菜单不可以删除，谢谢合作');
        }else{
            $.messager.alert('警告','警告消息');
            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.post(
                        "${proPath}/seemenu/deleteNote.action",
                        {id:id},
                        function (json) {
                            alert('确认删除');
                            $('#tt').tree('reload')

                        },
                        "json"
                    );
                }
            });
        }
 }

/* 添加*/
 function append() {
     flag="1";
     var selected = $('#tt').tree('getSelected');
     var d=$('#tt').tree('getSelected');
     var id=d.id;
      if(id==1){
          $('#tt').tree('append', {
              parent: selected.target,
              data: [{
                  text: '新建分类',
                  id: '100',
                  checked:true
              }],
          });
          var node = $('#tt').tree('find', 100);
          $('#tt').tree('beginEdit', node.target);
         /* var selected = $('#tt').tree('getSelected');
          var name=node.text;
          $.post(
              "{proPath}/seemenu/addMenuPlan.action",
              {name:name},
              "json"
          );*/
      }else{
          $.messager.alert('警告','此菜单不可以添加分类，谢谢合作');
      }
 }

/* 修改*/
function amend() {
    flag="2";
    var d=$('#tt').tree('getSelected');
    var id=d.id;
    if(id==1){
        alert("抱歉，此菜单不可以操作")
    }else{
        var node = $('#tt').tree('getSelected');
      $('#tt').tree('beginEdit', node.target);
    }
}

</script>
<body>

<form action="">
    总公司 ：
    <select id="cc" class="easyui-combobox" name="dept" style="width:100px;">
        <option value="aa">宏福测试</option>
        <!--<option>bitem2</option>-->
    </select>
    店铺名称
    <select id="c" class="easyui-combobox" name="dept" style="width:100px;">
        <option value="aa">中餐厅</option>
        <!--<option>bitem2</option>-->
    </select>
    <input type="button" value="查询">
</form>

<div id="ccc" class="easyui-layout" data-options="fit:true" style="width:600px;height:400px;">
    <div data-options="region:'west',title:'菜单类目管理',split:true,collapsible:false" style="width:180px;" >
   <ul id="tt">
       <div id="mm" class="easyui-menu" style="width:120px;">
           <div onclick="append()" data-options="iconCls:'icon-add'">添加</div>
           <div onclick="remove()" data-options="iconCls:'icon-remove'">删除</div>
           <div onclick="amend()" data-options="iconCls:'icon-remove'">修改</div>
       </div>
   </ul>

    </div>
    <div data-options="region:'center',title:'主要内容'" style="padding:5px;background:#eee;">
        所选菜单 ：<INPUT TYPE="text" id="wz">
        <table id="dg" ></table>
    </div>
</div>



</body>
</html>